package com.bkq.cb;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class ExchangeRateScreen extends Activity implements OnItemSelectedListener {
	String[] items={"VND", "USD", "EUR", "HKD"};
	String inp,outp;
	private SqliteServices service = null;
	private Cursor mCursor;
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.exchange_ratescreen);
	     
	     final EditText amount = (EditText) findViewById(R.id.amountTxt_exchangeRate);
	     final EditText result = (EditText) findViewById(R.id.resultTxt_exchangeRate);
	     final Button back = (Button)findViewById(R.id.backIma_exchangeRate);
	     final Button convert = (Button)findViewById(R.id.convertBnt_exchangeRate);
	     final Spinner spin=(Spinner)findViewById(R.id.inputSpinner_exchange_rate);
	     final Spinner spin1=(Spinner)findViewById(R.id.outputSpinner_exchange_rate);
	     spin.setOnItemSelectedListener(this);
	     
	     ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,items);
	     aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	     spin.setAdapter(aa);
	     spin1.setAdapter(aa);
	     back.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		})   ;
	     service = new SqliteServices(this);
	     service.openDatabase();
	     mCursor = service.queryData("select * from tblRateExchange");
	     
	     spin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
				// TODO Auto-generated method stub
				inp = items[arg2].toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	     spin1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				outp = items[arg2].toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	     
	     convert.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(inp.equals(outp)){
					result.setText("");
					String a = amount.getText().toString();
					result.append(a);
				}
				String q = amount.getText().toString();
				if(q.equals("")){
					Toast.makeText(ExchangeRateScreen.this, "Please fill in Amount", Toast.LENGTH_LONG).show();
				}
				else{
					if(mCursor.moveToFirst()){
						if((mCursor.getString(1).equals(inp))&&(mCursor.getString(2).equals(outp))){
							result.setText("");
							String a = mCursor.getString(3).toString();
							Float value = Float.valueOf(a);
							String b = amount.getText().toString();
							Float amount_value = Float.valueOf(b);
							Float cash = value*amount_value;
							String cash1 = String.valueOf(cash).toString();
							result.append(cash1);
						}	
						while(mCursor.moveToNext()){
							if((mCursor.getString(1).equals(inp))&&(mCursor.getString(2).equals(outp))){
								result.setText("");
								String a = mCursor.getString(3).toString();
								Float value = Float.valueOf(a);
								String b = amount.getText().toString();
								if(b.equals("")){
									Toast.makeText(ExchangeRateScreen.this, "abc", Toast.LENGTH_LONG).show();
								}
								Float amount_value = Float.valueOf(b);
								Float cash = value*amount_value;
								String cash1 = String.valueOf(cash).toString();
								result.append(cash1);
							}	
						}
					}
					
				}
			}
		});
	}
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		//Lay ten (items[position]);
	}
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
}
